********************************************************************************
** 	TITLE: e23_tornado_plots.do

**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Figure XXIII: Literature Comparisons
********************************************************************************
		set sortseed 13

use "$output_data/estimate.dta", clear

		//stay ignite_0 for Recidivism
	cap drop stay
	gen stay = stay_rec
	cap drop ignite_0
	gen ignite_0 = ignite_0_rec

*******Complier control means for number of 12 months recidivism.
//Code taken from "$code/c_complier_control_mean.do"
	
cap drop stay_day
gen stay_day = stay*30
gen less_than_3 = stay_day<=3

		cap gen return_12m_less_than_3 = return_12m*less_than_3
		ivreghdfe  return_12m_less_than_3 ( less_than_3 = i.D_remove_any) ///
			 if  return_12m>=0&D_ignite_proxy, ///
			cluster(inmate) a($design_control)
		local ccm_return_12m : di %8.3f _b[less_than_3]



				local controls " "
				
			* IV				
				ivreghdfe return_12m (ignite_0 stay = interact_proxy_any D_remove_any) `controls' D_ignite_proxy ///
					if return_12m>=0, ///
					cluster(inmate) ffirst a($design_control)
				eststo tslsreturn_12m0any
	
				//Creating locals for Tornado plot updates!
				local ignite_0return_12m : di %8.3f _b[ignite_0]
				local ignite_0return_12m_se : di %8.3f _se[ignite_0]
				local stayreturn_12m : di %8.3f _b[stay]
				local stayreturn_12m_se : di %8.3f _se[stay]

				
/*************************************
	A. Meta-analysis
*************************************/
	clear all
	set more off
	set scheme burd4	

/*************************************
	A. General settings
*************************************/	
** load data 
	import excel "$input_data/lit_review_03272024.xlsx",sheet("Economics") firstrow clear
	drop if benchmark_numbers == "Not comparable" | benchmark_numbers == "Null results"
	destring benchmark_numbers, replace
	drop if Author == "" // cleaning missing observations from the spreadsheet

		//benchmark_numbers
	replace benchmark_numbers = (`ignite_0return_12m'/`ccm_return_12m')*100 if ignite == 1 & Type == "Programming"
	replace benchmark_numbers = (`stayreturn_12m'/`ccm_return_12m')*100 if ignite == 1 & Type == "Incarceration"
		//lbenchmark_numbers 	
	replace lbenchmark_numbers = ((`ignite_0return_12m' - 1.96 * `ignite_0return_12m_se')/`ccm_return_12m')*100 if ignite == 1 & Type == "Programming"
	replace lbenchmark_numbers = ((`stayreturn_12m' - 1.96 * `stayreturn_12m_se')/`ccm_return_12m')*100 if ignite == 1 & Type == "Incarceration"
		
		//ubenchmark_numbers
	replace ubenchmark_numbers = ((`ignite_0return_12m' + 1.96 * `ignite_0return_12m_se')/`ccm_return_12m')*100 if ignite == 1 & Type == "Programming"
	replace ubenchmark_numbers = ((`stayreturn_12m' + 1.96 * `stayreturn_12m_se')/`ccm_return_12m')*100 if ignite == 1 & Type == "Incarceration"
	
** dropping Doleac et al. 2020 because of multiple papers and hard to format
	drop if Author == "Doleac et al. 2020"
** dropping Hjalmarsson 2009 because haven't been able to replicate the percentages reported in the paper
	drop if Author == "Hjalmarsson 2009"	
** dropping due to implausibly large effect size when performing linear transformation
	drop if Author == "Cunningham et al. 2024"
	
** clean paper info
	replace Author = trim(Author)
	replace Author = subinstr(Author,"(2022)","2022",.)
	
** set ylabel for graph
	ren Type Type_orig
	gen Type = "Incarceration" if Type_orig == "Incarceration"
	replace Type = "Programming" if Type_orig == "Programming"
	replace Type = "Alternative" if Type_orig == "Alternative"
	
	sort Type benchmark_numbers
	
** replace author by type
	gsort Type benchmark_numbers
	replace Author = "{bf:This paper}" if Author == "This paper"		
	replace Author = "{it:" + Subtype + ":}" + " " + Author if Type=="Non-incarceration"
	replace Author = "{bf:This paper}" if Author=="{bf:This paper}" /*please note that this is to vertically align the x-axis between the two panels*/
		
	gen temp_y_lab = (_N+1-_n)*3
	replace temp_y_lab = temp_y_lab-8 if Type=="Non-incarceration"
	
/*************************************
	B. Tornado Plots
*************************************/	
** Incarceration papers	

	forval j = 1/3 {
	    preserve
	    ** preparing y-axis label with citations of papers
				
		if `j'==1{
			local type "Alternative"
			local panel "C"
		}
		if `j'==2{
			local type "Incarceration"
			local panel "B"
		}
		if `j'==3{
			local type "Programming"
			local panel "A"

		}
		
		keep if Type == "`type'"
		qui su temp_y_lab
		local i_max=r(max)
		local i_min = r(min)
		labmask temp_y_lab, values(Author)
		
		local temp_min = `i_min'-1
		local temp_max = `i_max'+1
		
		local xlab xlab(-90(30)90, labsize(*.8)
		
		if `j'==3{
			local temp_min = `i_min'-1
			local temp_max = `i_max'+1
			local xlab xlab(-90(30)90, labsize(*.8)
		}
		
		local yscale yscale(range(`temp_min' `temp_max'))
		*https://www.stata.com/manuals13/dformat.pdf
		*https://www.statalist.org/forums/forum/general-stata-discussion/general/1539364-graph-displaying-too-many-decimal-places
		gen point_main = string(benchmark_numbers, "%3.1f") + "%"
		*https://www.stata.com/manuals13/g-3marker_label_options.pdf
		gen pos = 12
	**figure - Incarceration papers
		tw (rspike ubenchmark_numbers lbenchmark_numbers temp_y_lab if Jail == 1 & GeoCategory == "US", horizontal lc(red*1.3)) ///
			(rspike ubenchmark_numbers lbenchmark_numbers temp_y_lab if Jail == 1 & GeoCategory != "US", horizontal lc(navy*1.3)) ///	
			(rspike ubenchmark_numbers lbenchmark_numbers temp_y_lab if Jail == 0 & GeoCategory == "US", horizontal lc(navy*1.3)) ///	
			(rspike ubenchmark_numbers lbenchmark_numbers temp_y_lab if Jail == 0 & GeoCategory != "US", horizontal lc(navy*1.3)) ///	
			(scatter temp_y_lab benchmark_numbers if Jail == 1 & GeoCategory == "US" & ignite == 1, mc(red) m(O) mlabel(point_main) mlabv(pos) mlabc(red) msize(large)) ///
			(scatter temp_y_lab benchmark_numbers if Jail == 1 & GeoCategory == "US" & ignite == 0, mc(red) m(O) mlabel(point_main) mlabv(pos) mlabc(red) msize(small)) ///
			(scatter temp_y_lab benchmark_numbers if Jail == 1 & GeoCategory != "US", mc(navy) m(T) mlabel(point_main) mlabv(pos) mlabc(navy) msize(small)) ///
			(scatter temp_y_lab benchmark_numbers if Jail == 0 & GeoCategory == "US", mc(navy) m(Oh) mlabel(point_main) mlabv(pos) mlabc(navy) msize(small)) ///
			(scatter temp_y_lab benchmark_numbers if Jail == 0 & GeoCategory != "US", mc(navy) m(Th) mlabel(point_main) mlabv(pos) mlabc(navy) msize(small)) ///
			, ylab(`i_min'(3)`i_max',valuelabel nogrid labsize(*.8)) `yscale' ///
			xtitle("Effect Size (%)", size(*.7)) ytitle("") ///
			legend(order(6 "US | Jail" 7 "Non-US | Jail" 8 "US | Non-Jail" 9 "Non-US | Non-Jail")) legend(pos(1) cols(1) ring(0) size(vsmall)) ///
			`xlab' nogrid format(%3.0f)) xline(0, lc(black*0.2))
			
			graph export "$figures/Tornado_Panel`panel'_03272024.pdf",	replace		
		restore
	}
